import gcoord from "gcoord";

export const wgs84ToGcj02 = (lng: number, lat: number) => {
  return gcoord.transform<[number, number]>(
    [lng, lat],
    gcoord.WGS84,
    gcoord.GCJ02
  );
};

export const calculate = (origin: number[], destination: number[]) => {
  const pi = Math.PI / 180;
  const originLat = origin[1] * pi;
  const destinationLat = destination[1] * pi;
  const latitude = originLat - destinationLat;
  const longitude = origin[0] * pi - destination[0] * pi;
  const a = Math.pow(Math.sin(latitude / 2), 2);
  const b =
    Math.cos(originLat) *
    Math.cos(destinationLat) *
    Math.pow(Math.sin(longitude / 2), 2);
  return (
    Math.round(2 * Math.asin(Math.sqrt(a + b)) * 6378137.0 * 10000) / 10000
  );
};
